1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.testing.MinimalSet;
23  import com.google.common.collect.testing.features.CollectionFeature;
24  import com.google.common.collect.testing.features.CollectionSize;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.List;
29  
30  /**
31   * Tests {@link List#equals}.
32   *
33   * @author George van den Driessche
34   */
35  @GwtCompatible
36  public class ListEqualsTester<E> extends AbstractListTester<E> {
37    public void testEquals_otherListWithSameElements() {
38      assertTrue(
39          "A List should equal any other List containing the same elements.",
40          getList().equals(new ArrayList<E>(getOrderedElements())));
41    }
42  
43    @CollectionSize.Require(absent = CollectionSize.ZERO)
44    public void testEquals_otherListWithDifferentElements() {
45      ArrayList<E> other = new ArrayList<E>(getSampleElements());
46      other.set(other.size() / 2, getSubjectGenerator().samples().e3);
47      assertFalse(
48          "A List should not equal another List containing different elements.",
49          getList().equals(other));
50    }
51  
52    @CollectionSize.Require(absent = CollectionSize.ZERO)
53    public void testEquals_otherListContainingNull() {
54      List<E> other = new ArrayList<E>(getSampleElements());
55      other.set(other.size() / 2, null);
56      assertFalse("Two Lists should not be equal if exactly one of them has "
57          + "null at a given index.",
58          getList().equals(other));
59    }
60  
61    @CollectionSize.Require(absent = CollectionSize.ZERO)
62    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
63    public void testEquals_containingNull() {
64      ArrayList<E> elements = new ArrayList<E>(getSampleElements());
65      elements.set(elements.size() / 2, null);
66      collection = getSubjectGenerator().create(elements.toArray());
67      List<E> other = new ArrayList<E>(getSampleElements());
68      assertFalse("Two Lists should not be equal if exactly one of them has "
69          + "null at a given index.",
70          getList().equals(other));
71    }
72  
73    @CollectionSize.Require(absent = CollectionSize.ZERO)
74    public void testEquals_shorterList() {
75      Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
76      assertFalse("Lists of different sizes should not be equal.",
77          getList().equals(new ArrayList<E>(fewerElements)));
78    }
79  
80    public void testEquals_longerList() {
81      Collection<E> moreElements = getSampleElements(getNumElements() + 1);
82      assertFalse("Lists of different sizes should not be equal.",
83          getList().equals(new ArrayList<E>(moreElements)));
84    }
85  
86    public void testEquals_set() {
87      assertFalse("A List should never equal a Set.",
88          getList().equals(MinimalSet.from(getList())));
89    }
90  }